from django.shortcuts import render
from search.models import *
from userauth.models import *
from userauth.views import *
from django.shortcuts import HttpResponse, render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User

# Create your views here.
class Auth:
    def Login(request):
        if request.method == 'POST':
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return redirect("/")
        return render(request, "login_main.html")
    def Regist(request):
        if request.method == 'POST':
            username = request.POST.get('username')
            password = request.POST.get('password')
            realname = request.POST.get('realname')
            yxid = request.POST.get('yxid')
            same_name_user = User.objects.filter(username=username)
            if same_name_user:
                return render(request, "regist_main.html")
            new_user = User.objects.create_user(username,"standard@users.com",password)
            new_user.save()
            new_usr_id = User.objects.get(username=username)
            userreal = UserInformation(uid=new_usr_id.id,realname=realname,yxid=yxid)
            userreal.save()
            return redirect("/user/login")
        return render(request, "regist_main.html")
    def Logout(request):
        logout(request)
        return redirect("/user/login")
        
